package com.midea.ai.air.ui.more;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.ui.more.adapter.MoreSettingElectricityReportAdapter;
import com.midea.ai.air.view.wheelview.ArrayWheelAdapter;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelView;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hww on 2016/4/8.
 * 更多-电量
 */
public class MoreSettingElectricityReport extends BaseActivity implements View.OnClickListener {

    public static String MORE_SETTING_ELECTRICITY_YEAR = "electricity_year";
    public static String MORE_SETTING_ELECTRICITY_PERSON = "electricity_person";

    private TextView left_tv, tv_ele_limit;
    private ImageView iv_monthly_remind;
    private boolean isShow_remind;
    private View mConvertView;
    private PopupWindow pop_ele_limit;
    private ListView listView, listView_history;
    private List<String> list_data, list_data_history;
    private MoreSettingElectricityReportAdapter Adapter, Adapter_history;
    private RelativeLayout rl_electricity_limit, rl_left_back;

    private WheelView ele_limit_wheelview;
    String ele_String[] = new String[]{"不限", "10度", "20度", "30度", "40度", "50度", "60度", "70度", "80度", "90度", "100度", "150度", "200度", "250度", "300度", "350度", "400度", "450度", "500度"};

    private static final int UPDATE_ELE_LIMIT = 0X0000;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_ELE_LIMIT:
                    int index = (int) msg.obj;
                    Log.i("hwwlog", "handler 用电限额 结果：" + ele_String[index]);
                    tv_ele_limit.setText(ele_String[index]);
                    break;
                default:
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettingelectricityreport);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//判断API的版本
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(getResources().getColor(R.color.common_blue));

        init();
        initWheelView();
    }


    private void init() {
        left_tv = (TextView) findViewById(R.id.more_setting_electricity_tv_left);
        tv_ele_limit = (TextView) findViewById(R.id.more_setting_electricity_electricity_limit_tv_show);
        iv_monthly_remind = (ImageView) findViewById(R.id.more_setting_electricity_monthly_remind);
        listView = (ListView) findViewById(R.id.more_setting_electricity_gejiadiandianliang_listView);
        listView_history = (ListView) findViewById(R.id.more_setting_electricity_lishidianliang_listView);
        rl_electricity_limit = (RelativeLayout) findViewById(R.id.more_setting_electricity_electricity_limit_rl);
        rl_left_back = (RelativeLayout) findViewById(R.id.more_setting_electricity_rl_left);

        iv_monthly_remind.setOnClickListener(this);
        rl_left_back.setOnClickListener(this);

        getDate();
        mConvertView = getPOP_View();
        rl_electricity_limit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop_ele_limit = Creat_Popuwindow(v, mConvertView);
            }
        });

        Adapter = new MoreSettingElectricityReportAdapter(MoreSettingElectricityReport.this, list_data);
        Adapter_history = new MoreSettingElectricityReportAdapter(MoreSettingElectricityReport.this, list_data_history);

        listView.setAdapter(Adapter);
        listView_history.setAdapter(Adapter_history);

        //各家电 电量(度)
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                start_activity(MoreSettingElectricityReportPersonageEle.class, position);

            }
        });

        //历史电量
        listView_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                start_activity(MoreSettingElectricityReportShowEle.class, (11 - position));
                Log.i("hwwlog", "电量报告  position" + (11 - position));
            }
        });
    }

    private void start_activity(Class<?> cls, int Tag) {
        Intent intent = new Intent(MoreSettingElectricityReport.this, cls);
        Bundle bundle = new Bundle();
        bundle.putInt(MORE_SETTING_ELECTRICITY_YEAR, Tag);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private void getDate() {
        list_data = new ArrayList<String>();
        list_data_history = new ArrayList<String>();

        list_data.add("YA100");
        list_data.add("YA200");

        for (int i = 0; i < 6; i++) {
            list_data_history.add("2015年" + (11 - i) + "月");
        }
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();

        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_setting_electricity_rl_left:

                finish();
                break;
            case R.id.more_setting_electricity_monthly_remind:

                if (isShow_remind) {
                    isShow_remind = false;
                    iv_monthly_remind.setImageResource(R.drawable.more_setting_location_bt_on);
                } else {
                    isShow_remind = true;
                    iv_monthly_remind.setImageResource(R.drawable.more_setting_location_bt_off);
                }
                break;
//            case R.id.more_setting_electricity_electricity_limit_rl:
//                //用电限额
//
//                break;
            default:
                break;
        }
    }


    private void initWheelView() {
        ele_limit_wheelview = (WheelView) mConvertView.findViewById(R.id.item_pop_ele_temp);

        ele_limit_wheelview.setCyclic(true);
        ele_limit_wheelview.setAdapter(new ArrayWheelAdapter<>(ele_String));

        ele_limit_wheelview.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();

                Log.i("hwwlog", "用电限额 结果：" + ele_String[index]);
                handler.obtainMessage(UPDATE_ELE_LIMIT, index).sendToTarget();
//                handler.obtainMessage(UPDATE_ELE_LIMIT, index, index).sendToTarget();
            }
        });
    }


    private View getPOP_View() {
        LayoutInflater mLI = LayoutInflater.from(this);
        View convertView = mLI.inflate(R.layout.item_popupwindow_wheelview_electrity, null);
        return convertView;
    }


    /**
     * 新建 弹窗 POP
     *
     * @param v
     * @param convertView
     * @return
     */
    private PopupWindow Creat_Popuwindow(View v, View convertView) {
        PopupWindow popupWindow = null;
        if (popupWindow == null) {
//            LayoutInflater mLI = LayoutInflater.from(this);
//            View convertView = mLI.inflate(R.layout.item_popupwindow_wheelview_electrity, null);
            popupWindow = new PopupWindow(convertView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

        }

        ColorDrawable cD = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cD);
        // popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 产生背景变暗效果
        setAttributes(0.2f);

        popupWindow.setFocusable(true);// 设置可获取焦点
        popupWindow.setOutsideTouchable(true);// 设置点击外部可dismiss
        popupWindow.setAnimationStyle(R.style.pop_anim_style);
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        popupWindow.update();

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                setAttributes(1f);
            }
        });

        return popupWindow;
    }

    //设置弹窗POP的背景颜色
    private void setAttributes(float f) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = f;
        this.getWindow().setAttributes(lp);
    }

}
